import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 *
 * @author TQNINH
 */
public class CustomerList {
    FileOutputStream fout;
    ObjectOutputStream oos;
    DataOutputStream dout;
    FileWriter fw;
    BufferedWriter bw;
    FileReader fr;
    BufferedReader br;
    //Hashtable<String, Customer> ltCustomer;
    TreeMap<String, Customer> ltCustomer;
    
    public CustomerList() {
        //ltCustomer = new Hashtable();
        ltCustomer = new TreeMap();
    }
    
    public CustomerList(String i, String n, byte g, byte a) {
        ltCustomer = new TreeMap();
    }
    
    public void addList() {
        Customer objCustomer = new Customer();
        objCustomer.inputCustomer();
        ltCustomer.put(objCustomer.cusID, objCustomer);
    }
    
    public void displayList() {
        Set s = ltCustomer.keySet();
        Iterator i = s.iterator();
        while(i.hasNext()) {
            System.out.println(ltCustomer.get((String) i.next()).toString());
        }
    }
    
    public void saveToTextFile(String filename) {
        try {
            fw = new FileWriter(filename, true);
            bw = new BufferedWriter(fw);
            for(Map.Entry<String, Customer> entry : ltCustomer.entrySet()) {
                Customer objCus = ltCustomer.get(entry.getKey());
                bw.write(objCus.cusID + "##" +
                        objCus.name + "##" +
                        objCus.age + "##" +
                        objCus.gender + "\n");
            }
            bw.close();
            fw.close();
        } catch(IOException e) {
            
        }
        System.out.println("Save To Text File -> Done writing!");
    }
    
    public void saveToFileObjectIterator(String filename) {
        try {            
            fout = new FileOutputStream(filename, true);
            oos = new ObjectOutputStream(fout);
            Set s = ltCustomer.keySet();
            Iterator i = s.iterator();
            while(i.hasNext()) {
                oos.writeObject(ltCustomer.get((String)i.next()));
            }
            oos.close();
            fout.close();
        } catch (IOException e) {
            
        }
        System.out.println("Save To File Object Iterator -> Done writing!");
    }
    
    public void saveToFileObjectEntrySet(String filename) {
        try {
            fout = new FileOutputStream(filename, true);
            oos = new ObjectOutputStream(fout);
            for(Map.Entry<String, Customer> entry : ltCustomer.entrySet()) {
                oos.writeObject(ltCustomer.get(entry.getKey()));
            }
            oos.close();
            fout.close();
        } catch (IOException e) {
            
        }
        System.out.println("Save To File Object Entry Set -> Done writing!");
    }
    
    public void saveToBinFile(String filename) {
        try {
            fout = new FileOutputStream(filename, true);
            oos = new ObjectOutputStream(fout);
            for(Map.Entry<String, Customer> entry : ltCustomer.entrySet()) {
                Customer objCus = ltCustomer.get(entry.getKey());
                oos.writeUTF(objCus.cusID);
                oos.writeUTF(objCus.name);
                oos.writeByte(objCus.age);
                oos.writeByte(objCus.gender);
            }
            oos.close();
            fout.close();
        } catch(IOException e) {
            
        }
        System.out.println("Save To Bin File -> Done writing!");
    }
    
    public void readFileTextToList(String filename) {
        try {
            fr = new FileReader(filename);
            br = new BufferedReader(fr);
            String record = "";
            while((record = br.readLine()) != null) {
                Customer objCus = new Customer();
                StringTokenizer stk = new StringTokenizer(record, "##");
                //while(stk.hasMoreTokens()) {
                    //objCus.cusID = ;
                    //String test = stk.nextToken();
                    objCus.name = stk.nextToken();
                    objCus.age = Byte.parseByte(stk.nextToken());
                    objCus.gender = Byte.parseByte(stk.nextToken());
                    ltCustomer.put(objCus.cusID, objCus);
                //}
            }
            
        } catch(IOException e) {
            
        }
        System.out.println("Read File Text To List -> Read successful!");
    }
}
